package com.crr;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Productor {
    public static void main(String[] args) {
        //1创建连接工厂 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setVirtualHost("/");
        factory.setPort(5672);
        factory.setUsername("user");
        factory.setPassword("chen1116.");
        // factory.setHost("127.0.0.1");
        //2创建连接，资源自动释放
        try (
                //创建连接
                Connection conn = factory.newConnection();
                //创建信道
                Channel channel = conn.createChannel();


        ) { //3声明交换器
            String exName="logEx";
            channel.exchangeDeclare(exName, BuiltinExchangeType.DIRECT);
            //4声明队列
            String q1="queueError";
            channel.queueDeclare(q1,false,false,false,null);
            String q2="queueInfo";
            channel.queueDeclare(q2,false,false,false,null);
            //5绑定队列与交换器
            channel.queueBind(q1,exName,"error");
            channel.queueBind(q2,exName,"info");

            //6发送消息
            channel.basicPublish(exName,"error",null,"订单错误".getBytes());
            channel.basicPublish(exName,"error",null,"库存不足".getBytes());
            channel.basicPublish(exName,"info",null,"日志信息".getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }
    }
}
